import {
    View, FlatList, Dimensions, RefreshControl, TouchableOpacity,
    TouchableHighlight, Text, StyleSheet, Image, Alert
} from 'react-native';
import React, {PureComponent} from 'react';
import Urls, {getRealUrl} from '../const/Urls'
import LocalImg from '../const/Images'
import moment from 'moment'
import ActionSheet from 'antd-mobile-rn/lib/action-sheet'

/**
 * 码头泊位
 */
class PageMtbw extends PureComponent {

    constructor(props) {
        super(props);
        this.user = this.props.user;
        this.isLoadMore = true;
        this.pageNum = 1;
        this.state = {
            data: [],
            isLoading: true,
        }
    }

    componentDidMount() {
        this.setState({
            data: [],
        });
        this.isLoadMore = true;
        this.loadData();
    }


    render() {
        return (
            <View style={{flexDirection: 'column', flex: 1}}>

                <View style={{
                    flexDirection: 'row',
                    flex: 0,
                    height: 60,
                    alignItems: 'center',
                    justifyContent: 'center',
                    backgroundColor: '#0066FF'
                }}>
                    <View style={{flex: 1, padding: 10}}>
                        <TouchableOpacity onPress={() => this.props.navigation.pop()}>
                            <Image source={LocalImg['backImg']} style={{height: 30, width: 30, resizeMode: 'contain'}}/>
                        </TouchableOpacity>
                    </View>
                    <View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
                        <Text style={{fontSize: 20, color: '#ffffff'}}>码头泊位</Text>
                    </View>

                    <View style={{flex: 1, alignItems: 'flex-end', padding: 10}}>

                    </View>
                </View>


                <View style={{
                    flexDirection: 'row',
                    backgroundColor: '#f9f9f9',
                    padding: 6,
                    justifyContent: 'center',
                    alignItems: 'center'
                }}>
                    <View style={{flex: 1}}>
                        <Text style={{fontWeight: 'bold'}}>码头名称</Text>
                    </View>
                    <View style={{flex: 1}}>
                        <Text style={{fontWeight: 'bold'}}>类型</Text>
                    </View>
                    <View style={{flex: 1}}>
                        <Text style={{fontWeight: 'bold'}}>等级</Text>
                    </View>

                </View>


                <View style={{
                    flexDirection: 'row',
                    backgroundColor: '#f9f9f9',
                    padding: 6,
                    justifyContent: 'center',
                    alignItems: 'center'
                }}>
                    <View style={{flex: 1}}>
                        <Text style={{fontWeight: 'bold'}}>长度</Text>
                    </View>
                    <View style={{flex: 1}}>
                        <Text style={{fontWeight: 'bold'}}>走向</Text>
                    </View>
                    <View style={{flex: 1}}>
                        <Text style={{fontWeight: 'bold'}}>水深</Text>
                    </View>
                </View>


                <FlatList
                    data={this.state.data}
                    renderItem={(item, index) => this.renderRow(item, index)}
                    keyExtractor={this.keyExtractor}
                    ItemSeparatorComponent={this.separator}
                    ListFooterComponent={() => this.footerView()}
                    refreshControl={
                        <RefreshControl
                            refreshing={this.state.isLoading}
                            onRefresh={() => this.onRefresh()}
                        />
                    }/>

            </View>
        )
    }

    keyExtractor = (item, index) => item.ID + "";

    footerView() {
        return (
            <View style={{flexDirection: 'row', justifyContent: 'center', paddingTop: 20, paddingBottom: 30}}>
                <Text>到底了</Text>
            </View>
        )
    }

    loadMore() {
        if (!this.state.isLoading && this.isLoadMore) {
            this.loadData();
        }
    }

    onRefresh() {
        this.pageNum = 1;
        this.setState({
            data: [],
        });
        this.isLoadMore = true;
        this.loadData();
    }


    loadData() {
        this.setState({
            isLoading: true
        });
        const request = new Map();
        request.set('pageNum', this.pageNum)

        fetch(getRealUrl(Urls['mtbwUrl'], request), {
            method: "GET",
            headers: {
                Accept: "application/json",
                "Content-Type": "application/json"
            },
        })
            .then(response => response.json())
            .then(responseJson => {
                if (responseJson instanceof Array) {

                    this.setState({
                        data: this.state.data.concat(responseJson),
                    });

                    if (responseJson.length > 0) {
                        this.setState({
                            pageNum: this.state.pageNum + 1
                        });
                    } else {
                        this.isLoadMore = false;
                    }
                }
                this.setState({
                    isLoading: false
                })
            })
            .catch(error => {
                console.error(error);
                Alert.alert("error");
                this.setState({
                    isLoading: false
                });
                this.isLoadMore = true;
            });

    }


    separator(index) {
        return (
            <View key={index} style={{height: 1, backgroundColor: '#CCCCCC'}}/>
        )
    }

    processDateTime(date) {
        const m = moment(date)
        if (m.isValid()) {
            return m.format('DD/HHmm')
        } else
            return ""
    }


    renderRow({item, index}) {

        let bColor = index % 2 !== 0 ? "#f9f9f9" : "#ffffff";
        let show = item.VCDOCKNAME === '' && parseInt(item.CHDOCKCODE) === 0;
        let style = 'flex';
        if (show) {
            style = 'none'
        }
        return (
            <TouchableHighlight key={item.CHDOCKCODE} style={{display: style}}>
                <View style={[styles.text, {flex: 1, marginLeft: 10, marginTop: 10, justifyContent: 'center'}]}>
                    <View style={{flex: 1, flexDirection: 'row'}}>
                        <View style={{flex: 1}}>
                            <Text>{item.dock_name}</Text>
                        </View>
                        <View style={{flex: 1}}>
                            <Text>{item.dock_type_name}</Text>
                        </View>
                        <View style={{flex: 1}}>
                            <Text>{item.dock_level_min}</Text>
                        </View>
                    </View>
                    <View style={{flex: 1, flexDirection: 'row'}}>
                        <View style={{flex: 1}}>
                            <Text>{item.roof_length}</Text>
                        </View>
                        <View style={{flex: 1}}>
                            <Text>{item.dockzxzx}</Text>
                        </View>
                        <View style={{flex: 1}}>
                            <Text>{item.foreland_fact_deep}</Text>
                        </View>
                    </View>

                </View>
            </TouchableHighlight>
        )
    }


    enterDetail(id) {
        this.props.navigation.navigate('PageCkjhDetail', {
            id: id
        })
    }


    showActionSheet() {
        ActionSheet.showActionSheetWithOptions({
            options: ['全部', '上海', '洋山', '金山孚宝', '绿化山', '交接中心'],
            message: ''
        }, (buttonIndex) => {


        })
    }

}

const styles = StyleSheet.create({
    phone: {
        flex: 1,
        textDecorationLine: 'underline',
        color: '#0066FF',
    },
    text: {
        flex: 1
    },
    row: {
        flexDirection: 'column',
        justifyContent: 'center',
        padding: 10,
    },
});


export default PageMtbw;